package com.scaffold.commons.datasource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {

	@Override
	protected Object determineCurrentLookupKey() {
		return DataSourceSwitcher.getDataSource();
	}

}

/*	<!-- 配置动态数据源 -->
    <bean id="parentDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
        //***c3p0配置  
    </bean>  
     <!-- 主数据源-->  
    <bean id="masterDataSource" parent="parentDataSource">  
        <property name="driverClass" value="${master.jdbc.driverClassName}" />  
        <property name="jdbcUrl" value="${master.jdbc.url}" />  
        <property name="user" value="${master.jdbc.username}" />  
        <property name="password" value="${master.jdbc.password}" />  
    </bean>  
    <!-- 从数据源-->  
    <bean id="readOnlyDataSource" parent="parentDataSource">  
        <property name="driverClass" value="${slave.jdbc.driverClassName}" />  
        <property name="jdbcUrl" value="${slave.jdbc.url}" />  
        <property name="user" value="${slave.jdbc.username}" />  
        <property name="password" value="${slave.jdbc.password}" />  
    </bean>  
  
    <bean id="dataSource" class="com.company.datasource.DynamicDataSource">  
        <property name="targetDataSources">  
            <map key-type="java.lang.String">  
                <entry key="readOnly" value-ref="readOnlyDataSource" />  
            </map>  
        </property>  
        <property name="defaultTargetDataSource" ref="masterDataSource" />  
    </bean>  
*/